package com.welsmann.app.start.spider.process;

import java.util.Timer;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 爬虫启动类
 * @author Welsmann
 *
 */
public class Loader {
	
	private final static Log log = LogFactory.getLog(Loader.class);
	
	private static long period = 1L * 60 * 1000 * 60;
	
	static {
		XMLConfiguration serviceConfig;
		try {
			serviceConfig = new XMLConfiguration("config.xml");
			period = serviceConfig.getLong("fetch.period");
		} catch (ConfigurationException e) {
			log.error(e);
		} finally {
			serviceConfig = null;
		}
	}

	/**
	 * 启动爬虫
	 * @param args
	 */
	public static void main(String[] args) {
		Timer timer = new Timer(true);
		timer.schedule(new Spider(), 0, period);
		timer.schedule(new WeatherSpider(), 0, period);
		//timer.schedule(new Spider(), 0);
		while (true) {
			try {
				int ch = System.in.read();
				if (ch == 'c') {
					timer.cancel();
				}
			} catch (Exception e) {
				log.error(e);
			} 
		}
	}
}
